home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 09 - 1993 / 09.03 Mar 93 / Sound 101 / Sound101_main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-10  |  5.7 KB  |  243 lines  |  [TEXT/KAHL]

  1. /************************************************************************************
  2.  
  3. Sound101_main.c
  4.  
  5. © 1992 Praxitel, Inc. and Iggi Monahelis
  6.  
  7. This is the "main" for the Sound 101 project.  It has the minimum stuff to get a mac
  8. program going.  Basically, it does all the required mac initializations, sets up the menu
  9. bar, and then gets and processes events.
  10.  
  11. *************************************************************************************/
  12. #include "Sound101.h"
  13.  
  14.  
  15. extern int                 gDestroyChannel;
  16. extern SndChannelPtr    gSndChan;
  17. extern short             gFRefNum;
  18. extern long                gQuality;
  19.  
  20. /************************************************************************************
  21.  
  22. main
  23.  
  24. This is the main routine for the Sound 101 application, it does all the Mac initializations.
  25. Then calls the EventLoop procedure to get and process events.
  26.  
  27. *************************************************************************************/
  28. void main(void)
  29. {
  30.     InitGraf((Ptr) &thePort );
  31.     InitFonts();
  32.     InitWindows();
  33.     InitMenus();
  34.     TEInit();
  35.     InitDialogs(0L);
  36.     InitCursor();
  37.     
  38.     MaxApplZone();
  39.     SetUpMenus();
  40.  
  41.     EventLoop();        /* Do event processing until user quits */
  42.  
  43. }
  44.  
  45.  
  46. /************************************************************************************
  47.  
  48. SetUpMenus
  49.  
  50. Set up the menu bar for the Sound 101 application
  51.  
  52. *************************************************************************************/
  53. void SetUpMenus(void)
  54. {
  55.     MenuHandle    myMenu;
  56.     
  57.     myMenu = GetMenu(mApple);
  58.     
  59.     AddResMenu( myMenu, 'DRVR' );
  60.     InsertMenu( myMenu, 0 );
  61.     InsertMenu(GetMenu(mFile), 0) ;
  62.     DrawMenuBar();
  63. }
  64.  
  65. /************************************************************************************
  66.  
  67. EventLoop
  68.  
  69. Event handling for the Sound 101 application.
  70. Get events forever, and handle them by calling DoEvent.
  71.      
  72. ************************************************************************************/
  73. void EventLoop(void)
  74. {
  75.     Boolean        gotEvent;
  76.     EventRecord    event;
  77.  
  78.     do {
  79.         gotEvent = WaitNextEvent(everyEvent, &event, 50, 0);
  80.         if ( gotEvent ) {
  81.             DoEvent(&event);
  82.             }
  83.         /* if we are done with the sound, dispose the sound channel and close
  84.         the sound file.
  85.         */    
  86.          if ( gDestroyChannel ) { 
  87.             OSErr             myErr;
  88.             
  89.             InitCursor();
  90.             if (gSndChan)
  91.                 myErr = SndDisposeChannel( gSndChan, TRUE);
  92.             if (gFRefNum)
  93.                 myErr = FSClose(gFRefNum);
  94.             /* make sure we do it only once! */
  95.             gDestroyChannel = FALSE;
  96.             gSndChan = 0;
  97.             gFRefNum = 0;
  98.             } 
  99.     } while ( true );    /* loop forever */
  100. } /*EventLoop*/
  101.  
  102. /************************************************************************************
  103.  
  104. DoEvent
  105.  
  106. Determine the event type and dispatch accordingly
  107.  
  108. ************************************************************************************/
  109. void DoEvent(EventRecord *event)
  110. {
  111.     short        part, err;
  112.     WindowPtr    window;
  113.     char        key;
  114.  
  115.     switch ( event->what ) {
  116.         case nullEvent:
  117.             break;
  118.         case mouseDown:
  119.             part = FindWindow(event->where, &window);
  120.             switch ( part ) {
  121.                 case inMenuBar:             /* process a mouse menu command */
  122.                     DoMenuCommand(MenuSelect(event->where));
  123.                     break;
  124.                 case inSysWindow:           /* let the system handle the mouseDown */
  125.                     SystemClick(event, window);
  126.                     break;
  127.                 case inContent:
  128.                     break;
  129.                 case inDrag:                
  130.                     break;
  131.                 case inGoAway:
  132.                     break;
  133.                 case inGrow:
  134.                     break;
  135.                 case inZoomIn:
  136.                 case inZoomOut:
  137.                     break;
  138.             }
  139.             break;
  140.         case keyDown:
  141.         case autoKey:                       /* check for menukey equivalents */
  142.             key = event->message & charCodeMask;
  143.             if ( event->modifiers & cmdKey ) {    /* Command key down */
  144.                 /* if command-period was pressed, set our global to clean up
  145.                 the sound channel and close the open sound file
  146.                 */
  147.                 if ( key == '.' )  
  148.                     gDestroyChannel = TRUE;    /* command period was pressed */
  149.             
  150.             }
  151.             break;
  152.         case activateEvt:
  153.             break;
  154.         case updateEvt:
  155.             break;
  156.     }
  157. } /*DoEvent*/
  158.  
  159. /************************************************************************************
  160.  
  161. DoMenuCommand
  162.  
  163. Handles menu selections for the Sound 101 application
  164.          
  165. ************************************************************************************/
  166. void DoMenuCommand(long menuResult)
  167. {
  168.     short        menuID, menuItem;
  169.     short        itemHit, daRefNum;
  170.     Str255        daName;
  171.  
  172.     menuID = HiWord(menuResult);
  173.     menuItem = LoWord(menuResult);
  174.     switch ( menuID ) {
  175.         case mApple:
  176.             switch ( menuItem ) {
  177.                 case iAbout: {        /* bring up the About box */
  178.                     OSErr            err;
  179.                     GrafPtr            oldPort;
  180.                     Rect             tempRect;
  181.                     DialogPtr        aboutDlg;
  182.                     
  183.                     GetPort(&oldPort);
  184.                 
  185.                     aboutDlg = GetNewDialog(128, nil, (WindowPtr) -1);
  186.                     if ( aboutDlg ) {
  187.                         ShowWindow(aboutDlg);           /* Open a dialog box */
  188.                         SelectWindow(aboutDlg);         /* Lets see it */
  189.                         SetPort(aboutDlg);
  190.                         while (TRUE) {
  191.                             ModalDialog(NULL, &itemHit);
  192.                             if ( itemHit == 1 )
  193.                                 break;
  194.                         }
  195.                         DisposDialog(aboutDlg);
  196.                         SetPort(oldPort);
  197.                     }
  198.                 }
  199.                 break;
  200.  
  201.                 default:            /* all non-About items in this menu are DAs */
  202.                     GetItem(GetMHandle(mApple), menuItem, daName);
  203.                     daRefNum = OpenDeskAcc(daName);
  204.                     break;
  205.             }
  206.         break;
  207.         
  208.         case mFile:
  209.             switch ( menuItem ) {
  210.                 case iPlaySound:
  211.                     PlayASound();
  212.                     break;
  213.                 case iRecordsndSound:
  214.                     Record_snd_resource(gQuality);
  215.                     break;
  216.                 case iRecordAIFFSound:
  217.                     Record_AIFF_sound(gQuality);
  218.                     break;
  219.                 case iQuality:
  220.                     GetQuality();
  221.                     break;
  222.                 case iQuit:
  223.                     Terminate();
  224.                     break;
  225.             }
  226.             break;
  227.         }    
  228.             
  229.     HiliteMenu(0);                    /* unhighlight what MenuSelect (or MenuKey) hilited */
  230. }
  231.  
  232. /************************************************************************************
  233.  
  234. Terminate
  235.  
  236. Quit the app
  237.          
  238. ************************************************************************************/
  239. void Terminate(void)
  240. {
  241.         ExitToShell();                            /* exit if no cancellation */
  242. } /*Terminate*/
  243.